<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;

class Post extends Api
{
    protected $with = ['business','post_image','user_index'];
    //用户端-个人中心-我的帖子
    public function user_index()
    {
        $this->checkLogin();
        $post = param();
        //门店ID
        if(!isset($post['id'])) return msgerror('参数缺失');
        $limit = input('limit') ?: 20;
        $data = $this->model->where([
            ['user_index_id','=',$this->userinfo['id']],
            ['business_id','=',$post['id']]
        ])->with($this->with)->order('id desc')->paginate($limit);
        if(!empty($data) && !empty($data->toArray()))
        {
            $data = $data->toArray();
            foreach ($data['data'] as $k=>$v)
            {
                if(!empty($v['post_image'])){
                    foreach ($v['post_image'] as $kk=>$vv){
                        $data['data'][$k]['post_image'][$kk]['image'] = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] .$vv['image'];
                    }
                }
            }
        }
        return datasuccess($data);
    }
    //用户端-个人中心-删除帖子
    public function user_delete()
    {
        $this->checkLogin();
        $post = param();
        //帖子ID
        if(!isset($post['id'])) return msgerror('参数缺失');
        $data = $this->model->where('id',$post['id'])->find();
        if(!empty($data) && !empty($data->toArray())){
            $data = $data->toArray();
        }else{
            return msgerror('不存在此帖子数据');
        }
        if($data['user_index_id'] != $this->userinfo['id']) return msgerror('不是帖子所有人，删除失败');
        $this->model::destroy($post['id']);
        return datasuccess([],'删除成功');
    }
    //用户端-首页-热门帖子
    public function rm_index()
    {
        $post = param();
        if(!isset($post['id'])) return msgerror('参数缺失');
        $data = $this->model->where([
            ['business_id','=',$post['id']]
        ])->with($this->with)->order('browse desc')->limit(0,2)->select();
        if(!empty($data) && !empty($data->toArray()))
        {
            $data = $data->toArray();
            foreach ($data as $k=>$v)
            {
                if(!empty($v['post_image'])){
                    foreach ($v['post_image'] as $kk=>$vv){
                        $data[$k]['post_image'][$kk]['image'] = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] .$vv['image'];
                    }
                }
            }
        }
        return datasuccess($data);
    }
    //用户端-发帖
    public function add()
    {
        $this->checkLogin();
        $post = param();
        if(!empty($post)){
            //分类ID，内容
            if(!isset($post['post_class_id']) || !isset($post['content']) || !isset($post['business_id']))return msgerror('参数缺失');
            if(isset($post['images'])){
                $images = $post['images'];
                unset($post['images']);
            }
            $post['user_index_id'] = $this->userinfo['id'];
            $this->model->save($post);
            $id = $this->model->id;
            if(isset($images)){
                $images = explode(',',$images);
                $arr = [];
                foreach ($images as $k=>$v){
                    $arr[] = ['post_id'=>$id,'image'=>$v,'type'=>1];
                }
                app('app\api\model\PostImage')->saveAll($arr);
            }
            return datasuccess([],'发布成功');
        }else{
            return msgerror('参数缺失');
        }
    }
    //用户端-社区-帖子列表
    public function index()
    {
        $post = param();
        //门店ID，分类ID
        if(!isset($post['id']) || !isset($post['class_id'])) return msgerror('参数缺失');
        $limit = input('limit') ?: 20;
        $data = $this->model->where([
            ['business_id','=',$post['id']],
            ['post_class_id','=',$post['class_id']]
        ])->with($this->with)->order('id desc')->paginate($limit);
        if(!empty($data) && !empty($data->toArray()))
        {
            $data = $data->toArray();
            foreach ($data['data'] as $k=>$v)
            {
                if(!empty($v['post_image'])){
                    foreach ($v['post_image'] as $kk=>$vv){
                        $data['data'][$k]['post_image'][$kk]['image'] = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] .$vv['image'];
                    }
                }
            }
        }
        return datasuccess($data);
    }
    //详情接口
    public function view()
    {
        if (!input('post.id')) {
            return msgerror('缺少参数id');
        }
        $this->where[] = ['id', '=', input('post.id')];
        $list = $this->model->with($this->with)->where($this->where)->find()->toArray();
        urladd($list);
        if(!empty($this->with)){
            foreach ($this->with as $k=>$v) {
                if(is_string($v)){
                    urladd($list[$v]);
                }else{
                    urladd($list[$k]);
                }
            }
        }
        $list = $this->view_handel($list);
        return datasuccess($list);
    }

//    //删除数据
//    public function delete()
//    {
//        if (!input('post.id')) {
//            return msgerror('缺少参数id');
//        }
//        $id = input('post.id');
//        $data = $this->model->where('id',$id)->find()->toArray();
//        if($data['status'] == 1){
//            $this->model::update(['id'=>$id,'is_del'=>1]);
//        }else{
//            $this->model::destroy($id);
//        }
//        return datasuccess([], '删除成功');
//    }
}
